﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class OrderDetailBLL
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private static readonly IContractDA contractDA = DataAccess.CreateContractDA();
        private List<string> dataBaseID;
        private static readonly IIntransitDetailDA intransitDetailDA = DataAccess.CreateIntransitDetailDA();
        private static readonly IInventoryMgtDA inventoryMgtDA = DataAccess.CreateInventoryMgtDA();
        private static readonly ILotAttributeDA lotAttributeDA = DataAccess.CreateLotAttributeDA();
        private static readonly IOrdersDA orderDA = DataAccess.CreateOrdersDA();
        private static readonly IOrderDetailDA orderDetailDA = DataAccess.CreateOrderDetailDA();
        private static readonly IOwnerDA ownerDA = DataAccess.CreateOwnerDA();
        private static readonly IPackDA packDA = DataAccess.CreatePackDA();
        private static readonly IPreallocatePickDetailDA preallocatePickDetailDA = DataAccess.CreatePreallocatePickDetailDA();
        private PubParasInfo pubParasInfo;
        private static readonly IReceiptDetailDA receiptDetailDA = DataAccess.CreateReceiptDetailDA();
        private static readonly ISalesOrderDetailDA salesOrderDetailDA = DataAccess.CreateSalesOrderDetailDA();
        private static readonly ISkuDA skuDA = DataAccess.CreateSkuDA();
        private static readonly IOwnerTransferDA transferDA = DataAccess.CreateOwnerTransferDA();
        private static readonly IOwnerTransferDetailDA transferDetailDA = DataAccess.CreateOwnerTransferDetailDA();
        private static readonly IUserOwnerDA userOwner = DataAccess.CreateUserOwnerDA();

        public OrderDetailBLL()
        {
            this.pubParasInfo = null;
        }

        public OrderDetailBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public OrderDetailBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string CheckSkuIDExist(string orderID, string skuID)
        {
            string whLoginID = this.dataBaseID[0];
            if (orderDetailDA.CheckOrderDetailSkuExist(orderID, skuID, whLoginID))
            {
                return "Y";
            }
            return "N";
        }

        public string CreateOwnerTransferbyOrderIdLineID(string orderID, List<string> lineID)
        {
            Exception ex;
            string whLoginID = this.dataBaseID[0];
            string whID = string.Empty;
            string temp = string.Empty;
            string strGlobal = commonMethod.GetDataBaseIDByWhDBName(ConstUtils.CONST_GLOBAL_SETTING);
            try
            {
                List<OwnerTransferDetailInfo> OwnerTransferDetailList = new List<OwnerTransferDetailInfo>();
                int i = 0;
                while (lineID.Count > i)
                {
                    OrderDetailInfo orderDetailInfo = orderDetailDA.GetOrderDetailByID(orderID, lineID[i], whLoginID, this.pubParasInfo);
                    if ((orderDetailInfo != null) && (orderDetailInfo.Status.ToUpper() == "PARTPRE-ALLOCATED"))
                    {
                        whID = orderDetailInfo.WhID;
                        decimal transferQty = orderDetailInfo.OriginalQty - orderDetailInfo.PreallocatedQty;
                        LotAttributeInfo lotAttribute = new LotAttributeInfo {
                            SkuID = orderDetailInfo.SkuID
                        };
                        List<OwnerInfo> OwnerInfoList = userOwner.GetAuthorizedOwnerByUserID(this.pubParasInfo.UserID, strGlobal);
                        if ((OwnerInfoList != null) && (OwnerInfoList.Count > 0))
                        {
                            foreach (OwnerInfo ownerinfo in OwnerInfoList)
                            {
                                lotAttribute.OwnerID = lotAttribute.OwnerID + "'" + ownerinfo.OwnerID + "',";
                            }
                        }
                        if (string.IsNullOrEmpty(lotAttribute.OwnerID))
                        {
                            throw new Exception("用户没有找到有权限的货主!");
                        }
                        lotAttribute.OwnerID = lotAttribute.OwnerID.Substring(0, lotAttribute.OwnerID.Length - 1);
                        lotAttribute.Status = "OK";
                        string orderBy = "ORDER BY OWNER_ID DESC, QTY DESC ";
                        List<InventoryInfo> inventoryInfos = inventoryMgtDA.GetInventoryInfoByLotAttribute(lotAttribute, orderBy, this.dataBaseID[0]);
                        decimal totalInvQty = 0M;
                        foreach (InventoryInfo Invinfo in inventoryInfos)
                        {
                            if (Invinfo.OwnerID != orderDetailInfo.OwnerID)
                            {
                                OwnerTransferDetailInfo ownerTransferDetailInfo;
                                if (Invinfo.Qty > 0M)
                                {
                                    decimal invQty = ((((Invinfo.Qty - Invinfo.AllocatedQty) - Invinfo.PickedQty) - Invinfo.ArchiveQty) - Invinfo.ExpectedQty) - Invinfo.PickinprocessQty;
                                    if (invQty > 0M)
                                    {
                                        if (invQty >= transferQty)
                                        {
                                            totalInvQty = transferQty;
                                            transferQty = 0M;
                                        }
                                        else if (invQty > 0M)
                                        {
                                            transferQty -= invQty;
                                            totalInvQty = invQty;
                                        }
                                        else
                                        {
                                            continue;
                                        }
                                    }
                                }
                                ownerTransferDetailInfo = new OwnerTransferDetailInfo();
                                ownerTransferDetailInfo.WhID = whID;
                                ownerTransferDetailInfo.FromOwnerID = Invinfo.OwnerID;
                                ownerTransferDetailInfo.FromSkuID = Invinfo.SkuID;
                                ownerTransferDetailInfo.ToOwnerID = orderDetailInfo.OwnerID;
                                ownerTransferDetailInfo.ToSkuID = orderDetailInfo.SkuID;
                                ownerTransferDetailInfo.ToQty = totalInvQty;
                                ownerTransferDetailInfo.FromUomID = orderDetailInfo.UomID;
                                ownerTransferDetailInfo.FromPackID = orderDetailInfo.PackID;
                                ownerTransferDetailInfo.ToUomID = orderDetailInfo.UomID;
                                ownerTransferDetailInfo.ToPackID = orderDetailInfo.PackID;
                                ownerTransferDetailInfo.CreatedBy = this.pubParasInfo.UserID;
                                ownerTransferDetailInfo.UpdatedBy = this.pubParasInfo.UserID;
                                ownerTransferDetailInfo.ToSkuDescr = skuDA.GetSkuByID(ownerTransferDetailInfo.ToOwnerID, ownerTransferDetailInfo.ToSkuID, whLoginID).Descr;
                                ownerTransferDetailInfo.FromSkuDescr = skuDA.GetSkuByID(ownerTransferDetailInfo.FromOwnerID, ownerTransferDetailInfo.ToSkuID, whLoginID).Descr;
                                ownerTransferDetailInfo.FromLoc = "";
                                ownerTransferDetailInfo.ToLoc = "";
                                ownerTransferDetailInfo.ToLot = "";
                                ownerTransferDetailInfo.FromLot = "";
                                ownerTransferDetailInfo.FromLpnID = "";
                                ownerTransferDetailInfo.ToLpnID = "";
                                ownerTransferDetailInfo.FromQty = 0M;
                                ownerTransferDetailInfo.TransferLotInfo = new ReceiptLotInfo();
                                ownerTransferDetailInfo.TransferLotInfo.VendorID = "";
                                ownerTransferDetailInfo.TransferLotInfo.LotAttr01 = "";
                                ownerTransferDetailInfo.TransferLotInfo.LotAttr02 = "";
                                ownerTransferDetailInfo.TransferLotInfo.LotAttr03 = "";
                                ownerTransferDetailInfo.TransferLotInfo.LotAttr04 = "";
                                ownerTransferDetailInfo.TransferLotInfo.LotAttr05 = "";
                                ownerTransferDetailInfo.TransferLotInfo.LotAttr06 = "";
                                ownerTransferDetailInfo.TransferLotInfo.LotAttr07 = "";
                                ownerTransferDetailInfo.TransferLotInfo.LotAttr08 = "";
                                ownerTransferDetailInfo.TransferLotInfo.LotAttr09 = "";
                                ownerTransferDetailInfo.TransferLotInfo.LotAttr10 = "";
                                OwnerTransferDetailList.Add(ownerTransferDetailInfo);
                                if (transferQty == 0M)
                                {
                                    break;
                                }
                            }
                        }
                    }
                    i++;
                }
                if ((OwnerTransferDetailList == null) || (OwnerTransferDetailList.Count <= 0))
                {
                    return temp;
                }
                Dictionary<string, List<OwnerTransferDetailInfo>> ownerTransferDetailObjects = new Dictionary<string, List<OwnerTransferDetailInfo>>();
                List<string> OwnerIDList = new List<string>();
                foreach (OwnerTransferDetailInfo info in OwnerTransferDetailList)
                {
                    List<OwnerTransferDetailInfo> ownerTransferDetailInfoList = new List<OwnerTransferDetailInfo>();
                    if (!ownerTransferDetailObjects.ContainsKey(info.FromOwnerID + "@" + info.ToOwnerID))
                    {
                        ownerTransferDetailInfoList.Add(info);
                        OwnerIDList.Add(info.FromOwnerID + "@" + info.ToOwnerID);
                    }
                    else
                    {
                        ownerTransferDetailObjects.TryGetValue(info.FromOwnerID + "@" + info.ToOwnerID, out ownerTransferDetailInfoList);
                        ownerTransferDetailInfoList.Add(info);
                        ownerTransferDetailObjects.Remove(info.FromOwnerID + "@" + info.ToOwnerID);
                    }
                    ownerTransferDetailObjects.Add(info.FromOwnerID + "@" + info.ToOwnerID, ownerTransferDetailInfoList);
                }
                DataBase dataBase = new DataBase();
                DbConnection conn = dataBase.connection;
                conn.Open();
                DbTransaction tran = conn.BeginTransaction();
                try
                {
                    try
                    {
                        if ((OwnerIDList != null) && (OwnerIDList.Count > 0))
                        {
                            foreach (string info in OwnerIDList)
                            {
                                OwnerTransferInfo ownerTransferInfo;
                                List<OwnerTransferDetailInfo> ownerTransferDetailList = new List<OwnerTransferDetailInfo>();
                                ownerTransferDetailObjects.TryGetValue(info, out ownerTransferDetailList);
                                string[] strOwnerID = info.Split(new char[] { '@' });
                                ownerTransferInfo = new OwnerTransferInfo();
                                ownerTransferInfo.WhID = whID;
                                ownerTransferInfo.Status = "New";
                                ownerTransferInfo.TransferDate = DateTime.Now;
                                ownerTransferInfo.TransferType = "Owner";
                                ownerTransferInfo.GenIsInCharges = "N";
                                ownerTransferInfo.GenOtCharges = "N";
                                ownerTransferInfo.IsActive = "Y";
                                ownerTransferInfo.FromOwnerID = strOwnerID[0];
                                ownerTransferInfo.ToOwnerID = strOwnerID[1];
                                ownerTransferInfo.TransferID = commonMethod.GenNextNumStr(dataBase, tran, "TRANSFER", this.dataBaseID[0]);
                                ownerTransferInfo.CreatedBy = this.pubParasInfo.UserID;
                                ownerTransferInfo.UpdatedBy = ownerTransferInfo.CreatedBy;
                                ownerTransferInfo.CreatedDate = DateTime.Now;
                                ownerTransferInfo.UpdatedDate = DateTime.Now;
                                if ((transferDA.InsertOwnerTransfer(dataBase, tran, ownerTransferInfo, this.dataBaseID[0]) > 0) && ((ownerTransferDetailList != null) && (ownerTransferDetailList.Count > 0)))
                                {
                                    for (i = 0; ownerTransferDetailList.Count > i; i++)
                                    {
                                        ownerTransferDetailList[i].WhID = whID;
                                        ownerTransferDetailList[i].TransferID = ownerTransferInfo.TransferID;
                                        ownerTransferDetailList[i].LineID = (i + 1) * 10;
                                        ownerTransferDetailList[i].Status = "New";
                                        ownerTransferDetailList[i].TransferDate = DateTime.Now;
                                        if (transferDetailDA.InsertOwnerTransferDetail(dataBase, tran, ownerTransferDetailList[i], whLoginID) > 0)
                                        {
                                            temp = temp + ownerTransferInfo.TransferID + ";";
                                        }
                                    }
                                }
                            }
                            tran.Commit();
                        }
                        return temp;
                    }
                    catch (Exception exception1)
                    {
                        ex = exception1;
                        tran.Rollback();
                        temp = ex.Message;
                    }
                    return temp;
                }
                finally
                {
                    conn.Close();
                }
            }
            catch (Exception exception2)
            {
                ex = exception2;
                throw new Exception(ex.Message);
            }
            return temp;
        }

        public string DeleteDetail(List<string> orderIDs, List<string> LineIDs)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                int i;
                if (orderIDs.Count != LineIDs.Count)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F7100_002", this.pubParasInfo.UserLanguage);
                }
                List<OrderDetailInfo> orderDetailInfos = new List<OrderDetailInfo>();
                OrderDetailInfo orderDetailInfo = null;
                for (i = 0; i < orderIDs.Count; i++)
                {
                    orderDetailInfo = orderDetailDA.GetOrderDetailByID(dataBase, tran, orderIDs[i], LineIDs[i], this.dataBaseID[0], this.pubParasInfo);
                    orderDetailInfos.Add(orderDetailInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                if (orderDetailDA.DeleteOrderDetail(dataBase, tran, orderIDs, LineIDs, this.dataBaseID) > 0)
                {
                    List<IntransitDetailInfo> intransitDetailInfos = new List<IntransitDetailInfo>();
                    IntransitDetailInfo intransitDetailInfo = null;
                    for (i = 0; i < orderIDs.Count; i++)
                    {
                        intransitDetailInfo = intransitDetailDA.GetIntransitDetailByOrderID(dataBase, tran, orderIDs[i], LineIDs[i], this.dataBaseID[0]);
                        intransitDetailInfos.Add(intransitDetailInfo);
                    }
                    intransitDetailDA.DeleteIntransitDetailByOrderID(dataBase, tran, orderIDs, LineIDs, this.dataBaseID);
                    foreach (IntransitDetailInfo intransitDetail in intransitDetailInfos)
                    {
                        auditDataBLL.InsertAuditData<IntransitDetailInfo>(dataBase, tran, intransitDetail, null, AuditAction.Delete, this.pubParasInfo.UserID, "INTRANSIT_DETAIL", this.dataBaseID[0]);
                    }
                    for (i = 0; i < orderIDs.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<OrderDetailInfo>(dataBase, tran, orderDetailInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "ORDER_DETAIL", this.dataBaseID[0]);
                        string CS00003 = temp;
                        temp = CS00003 + orderIDs[i] + " , " + LineIDs[i] + ";";
                    }
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            if (temp.Length > 0)
            {
                return temp.Substring(0, temp.Length - 1);
            }
            return "";
        }

        public decimal ExchangeUom(PackInfo packInfo, string targetUom, decimal vqty, string flag)
        {
            float qty = Convert.ToSingle(vqty);
            float temp = 0f;
            if (qty == 0f)
            {
                temp = 0f;
            }
            else if (packInfo.PackUom1 == targetUom)
            {
                temp = qty;
            }
            else if (packInfo.PackUom2 == targetUom)
            {
                if (flag.ToUpper() == "R")
                {
                    temp = qty / packInfo.InnerPack;
                }
                else
                {
                    temp = packInfo.InnerPack * qty;
                }
            }
            else if (packInfo.PackUom3 == targetUom)
            {
                if (flag.ToUpper() == "R")
                {
                    temp = qty / packInfo.CaseCnt;
                }
                else
                {
                    temp = packInfo.CaseCnt * qty;
                }
            }
            else if (packInfo.PackUom4 == targetUom)
            {
                if (flag.ToUpper() == "R")
                {
                    temp = qty / packInfo.Pallet;
                }
                else
                {
                    temp = packInfo.Pallet * qty;
                }
            }
            else if (packInfo.PackUom5 == targetUom)
            {
                if (flag.ToUpper() == "R")
                {
                    temp = qty / packInfo.Cube;
                }
                else
                {
                    temp = packInfo.Cube * qty;
                }
            }
            else if (packInfo.PackUom6 == targetUom)
            {
                if (flag.ToUpper() == "R")
                {
                    temp = qty / packInfo.GrossWgt;
                }
                else
                {
                    temp = packInfo.GrossWgt * qty;
                }
            }
            else if (packInfo.PackUom7 == targetUom)
            {
                if (flag.ToUpper() == "R")
                {
                    temp = qty / packInfo.NetWgt;
                }
                else
                {
                    temp = packInfo.NetWgt * qty;
                }
            }
            else if (packInfo.PackUom8 == targetUom)
            {
                if (flag.ToUpper() == "R")
                {
                    temp = qty / packInfo.OtherUnit1;
                }
                else
                {
                    temp = packInfo.OtherUnit1 * qty;
                }
            }
            else if (packInfo.PackUom9 == targetUom)
            {
                if (flag.ToUpper() == "R")
                {
                    temp = qty / packInfo.OtherUnit2;
                }
                else
                {
                    temp = packInfo.OtherUnit2 * qty;
                }
            }
            return Convert.ToDecimal(temp);
        }

        public OrderDetailInfo GetOrderDetailByID(string OrderID, string LineID)
        {
            OrderDetailInfo orderDetailInfo = orderDetailDA.GetOrderDetailByID(OrderID, LineID, this.dataBaseID[0], this.pubParasInfo);
            if (orderDetailInfo != null)
            {
                PackInfo packInfo = packDA.GetPackByID(orderDetailInfo.PackID, this.dataBaseID[0]);
                orderDetailInfo.OriginalQty = this.ExchangeUom(packInfo, orderDetailInfo.UomID, orderDetailInfo.OriginalQty, "R");
                orderDetailInfo.ShippedQty = this.ExchangeUom(packInfo, orderDetailInfo.UomID, orderDetailInfo.ShippedQty, "R");
            }
            return orderDetailInfo;
        }

        public DataSet GetOrderDetailInfoByPagerQueryEntity(OrderDetailQueryEntity orderDetailQueryEntity)
        {
            List<string> strWhere = new List<string>();
            if (!string.IsNullOrEmpty(orderDetailQueryEntity.SkuID))
            {
                strWhere.Add(" OD.SKU_ID " + orderDetailQueryEntity.SkuID);
            }
            if (!string.IsNullOrEmpty(orderDetailQueryEntity.Status))
            {
                strWhere.Add(" OD.STATUS " + orderDetailQueryEntity.Status);
            }
            DataSet DataSetOrderDetailInfo = orderDetailDA.GetOrderDetailByPagerQueryList(strWhere, orderDetailQueryEntity, this.dataBaseID[0], this.pubParasInfo);
            InboundHelper inboundHelper = new InboundHelper();
            OutboundMgtBLL outBoundMgtBll = new OutboundMgtBLL();
            Dictionary<string, PackInfo> packInfoList = new Dictionary<string, PackInfo>();
            if (((DataSetOrderDetailInfo != null) && (DataSetOrderDetailInfo.Tables.Count > 2)) && (DataSetOrderDetailInfo.Tables[2].Rows.Count > 2))
            {
                Dictionary<string, SkuInfo> skuInfoList = new Dictionary<string, SkuInfo>();
                Dictionary<string, List<LotAttributeInfo>> lotAttributeInfoList = new Dictionary<string, List<LotAttributeInfo>> {  };
                for (int i = 0; DataSetOrderDetailInfo.Tables[2].Rows.Count > i; i++)
                {
                    string strExpiryType = string.Empty;
                    SkuInfo skuInfo = null;
                    if (skuInfoList.ContainsKey(DataSetOrderDetailInfo.Tables[2].Rows[i]["OWNER_ID"].ToString() + DataSetOrderDetailInfo.Tables[2].Rows[i]["SKU_ID"].ToString()))
                    {
                        skuInfo = skuInfoList[DataSetOrderDetailInfo.Tables[2].Rows[i]["OWNER_ID"].ToString() + DataSetOrderDetailInfo.Tables[2].Rows[i]["SKU_ID"].ToString()];
                    }
                    else
                    {
                        skuInfo = skuDA.GetSkuByID(DataSetOrderDetailInfo.Tables[2].Rows[i]["OWNER_ID"].ToString(), DataSetOrderDetailInfo.Tables[2].Rows[i]["SKU_ID"].ToString(), this.dataBaseID[0]);
                        skuInfoList.Add(DataSetOrderDetailInfo.Tables[2].Rows[i]["OWNER_ID"].ToString() + DataSetOrderDetailInfo.Tables[2].Rows[i]["SKU_ID"].ToString(), skuInfo);
                    }
                    if ((skuInfo != null) && (skuInfo.ShelfLifeIndicator == "Y"))
                    {
                        DateTime dtExpiry;
                        DateTime dtExpiry1;
                        List<LotAttributeInfo> lotList = null;
                        if (lotAttributeInfoList.ContainsKey(DataSetOrderDetailInfo.Tables[2].Rows[i]["OWNER_ID"].ToString() + DataSetOrderDetailInfo.Tables[2].Rows[i]["SKU_ID"].ToString()))
                        {
                            lotList = lotAttributeInfoList[DataSetOrderDetailInfo.Tables[2].Rows[i]["OWNER_ID"].ToString() + DataSetOrderDetailInfo.Tables[2].Rows[i]["SKU_ID"].ToString()];
                        }
                        else
                        {
                            lotList = lotAttributeDA.GetInvLotAttributeByID(DataSetOrderDetailInfo.Tables[2].Rows[i]["OWNER_ID"].ToString(), DataSetOrderDetailInfo.Tables[2].Rows[i]["SKU_ID"].ToString(), this.dataBaseID[0]);
                            lotAttributeInfoList.Add(DataSetOrderDetailInfo.Tables[2].Rows[i]["OWNER_ID"].ToString() + DataSetOrderDetailInfo.Tables[2].Rows[i]["SKU_ID"].ToString(), lotList);
                        }
                        if (skuInfo.ShelfLifeCodeType.ToUpper() == "E")
                        {
                            if (skuInfo.ShelfLife > 0)
                            {
                                dtExpiry = DateTime.Now;
                                dtExpiry1 = DateTime.Now.AddDays(Convert.ToDouble(skuInfo.ShelfLife));
                                DateTime dt = new DateTime();
                                foreach (LotAttributeInfo lotInfo in lotList)
                                {
                                    if (lotInfo.ExpiryDate != dt)
                                    {
                                        if ((dtExpiry <= lotInfo.ExpiryDate) && (dtExpiry1 >= lotInfo.ExpiryDate))
                                        {
                                            strExpiryType = "ABoutToExpire";
                                        }
                                        else if (dtExpiry > lotInfo.ExpiryDate)
                                        {
                                            strExpiryType = "Expired";
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                        else if (skuInfo.ShelfLife > 0)
                        {
                            dtExpiry = DateTime.Now;
                            dtExpiry1 = DateTime.Now.AddDays(Convert.ToDouble(skuInfo.ShelfLife));
                            foreach (LotAttributeInfo lotInfo in lotList)
                            {
                                if ((dtExpiry <= lotInfo.ProduceDate) && (dtExpiry1 >= lotInfo.ProduceDate))
                                {
                                    strExpiryType = "ABoutToExpire";
                                }
                                else if (dtExpiry > lotInfo.ProduceDate)
                                {
                                    strExpiryType = "Expired";
                                    break;
                                }
                            }
                        }
                    }
                    DataSetOrderDetailInfo.Tables[2].Rows[i]["EXPIRY_TYPE"] = strExpiryType;
                    PackInfo packInfo = null;
                    if (packInfoList.ContainsKey(DataSetOrderDetailInfo.Tables[2].Rows[i]["PACK_ID"].ToString()))
                    {
                        packInfo = packInfoList[DataSetOrderDetailInfo.Tables[2].Rows[i]["PACK_ID"].ToString()];
                    }
                    else
                    {
                        packInfo = packDA.GetPackByID(DataSetOrderDetailInfo.Tables[2].Rows[i]["PACK_ID"].ToString(), this.dataBaseID[0]);
                        packInfoList.Add(DataSetOrderDetailInfo.Tables[2].Rows[i]["PACK_ID"].ToString(), packInfo);
                    }
                    DataSetOrderDetailInfo.Tables[2].Rows[i]["ORIGINAL_QTY"] = inboundHelper.ExchangeUom(packInfo, DataSetOrderDetailInfo.Tables[2].Rows[i]["UOM_ID"].ToString(), Convert.ToDecimal(DataSetOrderDetailInfo.Tables[2].Rows[i]["ORIGINAL_QTY"]), "R");
                    DataSetOrderDetailInfo.Tables[2].Rows[i]["OPEN_QTY"] = inboundHelper.ExchangeUom(packInfo, DataSetOrderDetailInfo.Tables[2].Rows[i]["UOM_ID"].ToString(), Convert.ToDecimal(DataSetOrderDetailInfo.Tables[2].Rows[i]["OPEN_QTY"]), "R");
                    DataSetOrderDetailInfo.Tables[2].Rows[i]["SHIPPED_QTY"] = inboundHelper.ExchangeUom(packInfo, DataSetOrderDetailInfo.Tables[2].Rows[i]["UOM_ID"].ToString(), Convert.ToDecimal(DataSetOrderDetailInfo.Tables[2].Rows[i]["SHIPPED_QTY"]), "R");
                    DataSetOrderDetailInfo.Tables[2].Rows[i]["ADJUSTED_QTY"] = inboundHelper.ExchangeUom(packInfo, DataSetOrderDetailInfo.Tables[2].Rows[i]["UOM_ID"].ToString(), Convert.ToDecimal(DataSetOrderDetailInfo.Tables[2].Rows[i]["ADJUSTED_QTY"]), "R");
                    DataSetOrderDetailInfo.Tables[2].Rows[i]["PREALLOCATED_QTY"] = inboundHelper.ExchangeUom(packInfo, DataSetOrderDetailInfo.Tables[2].Rows[i]["UOM_ID"].ToString(), Convert.ToDecimal(DataSetOrderDetailInfo.Tables[2].Rows[i]["PREALLOCATED_QTY"]), "R");
                    DataSetOrderDetailInfo.Tables[2].Rows[i]["ALLOCATED_QTY"] = inboundHelper.ExchangeUom(packInfo, DataSetOrderDetailInfo.Tables[2].Rows[i]["UOM_ID"].ToString(), Convert.ToDecimal(DataSetOrderDetailInfo.Tables[2].Rows[i]["ALLOCATED_QTY"]), "R");
                    DataSetOrderDetailInfo.Tables[2].Rows[i]["PICKED_QTY"] = inboundHelper.ExchangeUom(packInfo, DataSetOrderDetailInfo.Tables[2].Rows[i]["UOM_ID"].ToString(), Convert.ToDecimal(DataSetOrderDetailInfo.Tables[2].Rows[i]["PICKED_QTY"]), "R");
                }
            }
            return DataSetOrderDetailInfo;
        }

        public string GetOrderDetailStatus(OrderDetailInfo orderDetail)
        {
            string status = OrderStatus.New;
            if (orderDetail != null)
            {
                decimal orderQty = ((orderDetail.PreallocatedQty + orderDetail.AllocatedQty) + orderDetail.PickedQty) + orderDetail.ShippedQty;
                if (orderQty < orderDetail.OriginalQty)
                {
                    orderQty = orderDetail.OriginalQty;
                }
                if ((orderDetail.PreallocatedQty > 0M) && (orderDetail.PreallocatedQty < orderQty))
                {
                    status = OrderStatus.PartPreAllocated;
                }
                if ((orderDetail.PreallocatedQty > 0M) && (orderDetail.PreallocatedQty >= orderQty))
                {
                    status = OrderStatus.PreAllocated;
                }
                if ((orderDetail.AllocatedQty > 0M) && (orderDetail.AllocatedQty < orderQty))
                {
                    status = OrderStatus.PartAllocated;
                }
                if ((orderDetail.AllocatedQty > 0M) && (orderDetail.AllocatedQty >= orderQty))
                {
                    status = OrderStatus.Allocated;
                }
                if ((orderDetail.PickedQty > 0M) && (orderDetail.PickedQty < orderQty))
                {
                    status = OrderStatus.PartPicked;
                }
                if ((orderDetail.PickedQty > 0M) && (orderDetail.PickedQty >= orderQty))
                {
                    status = OrderStatus.Picked;
                }
                if ((orderDetail.ShippedQty > 0M) && (orderDetail.ShippedQty < orderQty))
                {
                    status = OrderStatus.PartShipped;
                }
                if ((orderDetail.ShippedQty > 0M) && (orderDetail.ShippedQty >= orderQty))
                {
                    status = OrderStatus.Shipped;
                }
            }
            orderDetail.Status = status;
            return status;
        }

        public string GetOrderDetailStatus(string orderID, int orderLineID, string whLoginID)
        {
            OrderDetailInfo orderDetail = orderDetailDA.GetOrderDetailByID(orderID, orderLineID.ToString(), whLoginID, this.pubParasInfo);
            return this.GetOrderDetailStatus(orderDetail);
        }

        public string GetOrderDetailStatus(DataBase dataBase, DbTransaction tran, string orderID, int orderLineID, string whLoginID)
        {
            OrderDetailInfo orderDetail = orderDetailDA.GetOrderDetailByID(dataBase, tran, orderID, orderLineID.ToString(), whLoginID, this.pubParasInfo);
            return this.GetOrderDetailStatus(orderDetail);
        }

        public string GetOrderDetailStatus(DataBase dataBase, DbTransaction tran, string orderID, string lineID, string whLoginID)
        {
            string status = string.Empty;
            OrderDetailInfo orderDetail = orderDetailDA.GetOrderDetailByID(dataBase, tran, orderID, lineID, whLoginID, this.pubParasInfo);
            if (orderDetail != null)
            {
                status = this.GetOrderDetailStatus(orderDetail);
            }
            return status;
        }

        public string InserOrderDetailByReceipt(string orderID, List<string> receiptID, List<string> receiptLineID)
        {
            string temp = string.Empty;
            int iLineID = commonMethod.GetMaxLineID("ORDER_DETAIL", orderID, this.dataBaseID[0]);
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; receiptID.Count > i; i++)
                {
                    ReceiptDetailInfo receiptDetailinfo = receiptDetailDA.GetReceiptDetailByID(dataBase, tran, receiptID[i], receiptLineID[i], this.dataBaseID[0], this.pubParasInfo);
                    if (receiptDetailinfo != null)
                    {
                        iLineID += 10;
                        OrderDetailInfo orderDetailInfo = new OrderDetailInfo {
                            OrderID = orderID,
                            LineID = iLineID,
                            SkuID = receiptDetailinfo.SkuID,
                            OwnerID = receiptDetailinfo.OwnerID,
                            AdjustedQty = 0M,
                            AllocatedQty = 0M,
                            AllocateRuleID = "STD",
                            AllocateStrategyType = "",
                            AltSkuID = receiptDetailinfo.AltSkuID,
                            BuyerPo = "",
                            CartonBreak = "",
                            CartonGroup = "",
                            CartonQtyBreak = 0,
                            ComponentQty = 0M,
                            ComponentReference = "",
                            CreatedBy = this.pubParasInfo.UserID,
                            EffectiveDate = receiptDetailinfo.EffectiveDate,
                            EncasedQty = 0M,
                            ExpiryDate = receiptDetailinfo.ReceiptLot.ExpiryDate,
                            ExpiryType = "",
                            ExtendedPrice = receiptDetailinfo.ExtendedPrice,
                            ExternalLocSequence = "",
                            ExternalLot = receiptDetailinfo.ExternalLot,
                            ExternLineID = receiptDetailinfo.ReceiptLineID.ToString(),
                            ExternOrderID = receiptDetailinfo.ReceiptID,
                            IntransitID = "",
                            IsSubstitute = 0,
                            LineType = "",
                            Lot = ""
                        };
                        if (receiptDetailinfo.ReceiptLot != null)
                        {
                            orderDetailInfo.ExternalLot = receiptDetailinfo.ReceiptLot.ExternalLot;
                            orderDetailInfo.LotAttr01 = receiptDetailinfo.ReceiptLot.LotAttr01;
                            orderDetailInfo.LotAttr02 = receiptDetailinfo.ReceiptLot.LotAttr02;
                            orderDetailInfo.LotAttr03 = receiptDetailinfo.ReceiptLot.LotAttr03;
                            orderDetailInfo.LotAttr04 = receiptDetailinfo.ReceiptLot.LotAttr04;
                            orderDetailInfo.LotAttr05 = receiptDetailinfo.ReceiptLot.LotAttr05;
                            orderDetailInfo.LotAttr06 = receiptDetailinfo.ReceiptLot.LotAttr06;
                            orderDetailInfo.LotAttr07 = receiptDetailinfo.ReceiptLot.LotAttr07;
                            orderDetailInfo.LotAttr08 = receiptDetailinfo.ReceiptLot.LotAttr08;
                            orderDetailInfo.LotAttr09 = receiptDetailinfo.ReceiptLot.LotAttr09;
                            orderDetailInfo.LotAttr10 = receiptDetailinfo.ReceiptLot.LotAttr10;
                            orderDetailInfo.ProduceDate = receiptDetailinfo.ReceiptLot.ProduceDate;
                        }
                        orderDetailInfo.LpnID = receiptDetailinfo.LpnID;
                        orderDetailInfo.MinShipperCent = 0;
                        orderDetailInfo.OkToSubstitute = 0;
                        orderDetailInfo.OpenQty = 0M;
                        orderDetailInfo.OppRequest = "";
                        orderDetailInfo.OrigCaseQty = 0M;
                        orderDetailInfo.OriginalLineID = "";
                        orderDetailInfo.OriginalQty = receiptDetailinfo.ReceivedQty;
                        orderDetailInfo.OriginalSku = "";
                        orderDetailInfo.OrigPalletQty = 0M;
                        orderDetailInfo.PackID = receiptDetailinfo.PackID;
                        orderDetailInfo.PalletID = receiptDetailinfo.PalletID;
                        orderDetailInfo.PickCode = "";
                        orderDetailInfo.PickedQty = 0M;
                        orderDetailInfo.PickingInstructions = "";
                        orderDetailInfo.PreallocatedQty = 0M;
                        orderDetailInfo.PreallocateRuleID = "STD";
                        orderDetailInfo.ProductCube = 0.0;
                        orderDetailInfo.ProductWeight = 0.0;
                        orderDetailInfo.QcAutoAdjust = "";
                        orderDetailInfo.QcRequired = "";
                        orderDetailInfo.QtyIntransit = 0M;
                        orderDetailInfo.ReceiptDate = receiptDetailinfo.ReceiptDate;
                        orderDetailInfo.Remark = receiptDetailinfo.Remark;
                        orderDetailInfo.Rotation = "";
                        orderDetailInfo.ShelfLife = 0;
                        orderDetailInfo.ShipGroup01 = "";
                        orderDetailInfo.ShipGroup02 = "";
                        orderDetailInfo.ShipGroup03 = "";
                        orderDetailInfo.ShippedQty = 0M;
                        orderDetailInfo.SkuRotation = "STD";
                        orderDetailInfo.Status = "New";
                        orderDetailInfo.SubFlag = "";
                        orderDetailInfo.TariffID = receiptDetailinfo.TariffID;
                        orderDetailInfo.Tax01 = 0.0;
                        orderDetailInfo.Tax02 = 0.0;
                        orderDetailInfo.Udf1 = receiptDetailinfo.Udf1;
                        orderDetailInfo.Udf2 = receiptDetailinfo.Udf2;
                        orderDetailInfo.Udf3 = receiptDetailinfo.Udf3;
                        orderDetailInfo.Udf4 = receiptDetailinfo.Udf4;
                        orderDetailInfo.Udf5 = receiptDetailinfo.Udf5;
                        orderDetailInfo.Udf6 = receiptDetailinfo.Udf6;
                        orderDetailInfo.Udf7 = receiptDetailinfo.Udf7;
                        orderDetailInfo.Udf8 = receiptDetailinfo.Udf8;
                        orderDetailInfo.Udf9 = receiptDetailinfo.Udf9;
                        orderDetailInfo.Udf10 = receiptDetailinfo.Udf10;
                        orderDetailInfo.UnEncasedQty = 0M;
                        orderDetailInfo.UnitPrice = receiptDetailinfo.UnitPrice;
                        orderDetailInfo.UomID = receiptDetailinfo.UomID;
                        orderDetailInfo.UpdatedBy = orderDetailInfo.CreatedBy;
                        orderDetailInfo.UpdateSource = "";
                        orderDetailInfo.VendorID = receiptDetailinfo.VendorID;
                        orderDetailInfo.WorkOrderID = "";
                        orderDetailInfo.WpReleased = "";
                        orderDetailDA.InsertOrderDetail(dataBase, tran, orderDetailInfo, this.dataBaseID[0]);
                        string CS00003 = temp;
                        temp = CS00003 + receiptID[i] + ", 行号" + receiptLineID[i] + ";";
                    }
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }

        public string InsertOrderDetail(OrderDetailInfo orderDetailInfo)
        {
            if (!skuDA.CheckSkuIDUnique(orderDetailInfo.OwnerID, orderDetailInfo.SkuID, this.dataBaseID[0]))
            {
                PublicMethod.GetInstance().ShowAlertMessage("F7100_001", this.pubParasInfo.UserLanguage, new List<string> { orderDetailInfo.SkuID });
            }
            if (orderDetailDA.CheckOrderDetailIDUnique(orderDetailInfo.OrderID, orderDetailInfo.LineID.ToString(), this.dataBaseID[0]))
            {
                PublicMethod.GetInstance().ShowAlertMessage("F7100_006", this.pubParasInfo.UserLanguage, new List<string> { orderDetailInfo.OrderID, orderDetailInfo.LineID.ToString() });
            }
            orderDetailInfo.UpdatedBy = this.pubParasInfo.UserID;
            orderDetailInfo.CreatedDate = DateTime.Now;
            orderDetailInfo.UpdatedDate = DateTime.Now;
            InboundHelper inboundHelper = new InboundHelper();
            AuditDataBLL auditDataBLL = new AuditDataBLL();
            orderDetailInfo.OriginalQty = inboundHelper.ExchangeUom(orderDetailInfo.PackID, orderDetailInfo.UomID, orderDetailInfo.OriginalQty, "W", this.dataBaseID[0]);
            OrdersInfo ordersInfo = orderDA.GetOrdersByID(orderDetailInfo.OrderID, this.dataBaseID[0], this.pubParasInfo);
            decimal contractDeposit = 0M;
            decimal invAvailableTotalPrice = 0M;
            decimal unShippingAmount = 0M;
            TradePartnerInfo tradePartnerInfo = ownerDA.GetTradePartnerByID(orderDetailInfo.OwnerID, this.dataBaseID[0]);
            if ((tradePartnerInfo.Finance_Logistical_Ctrl == "Y") && (tradePartnerInfo.Finance_Logistical_Calc_By.ToUpper() == "SKUPRICE"))
            {
                ContractInfo contractInfo = contractDA.GetContractByOwnerID(orderDetailInfo.OwnerID, "Y", this.dataBaseID[0]);
                if (contractInfo == null)
                {
                    contractDeposit = 0M;
                }
                else
                {
                    contractDeposit = contractInfo.Deposit;
                }
                invAvailableTotalPrice = salesOrderDetailDA.GetOwnerAllInvDeposit(orderDetailInfo.OwnerID, this.dataBaseID[0]);
                unShippingAmount = salesOrderDetailDA.GetAllOrderDetailUnshippingAmount(orderDetailInfo.OwnerID, this.dataBaseID[0]);
                SkuInfo skuInfo = skuDA.GetSkuByID(orderDetailInfo.OwnerID, orderDetailInfo.SkuID, this.dataBaseID[0]);
                unShippingAmount += skuInfo.CostPrice * orderDetailInfo.OriginalQty;
                if ((invAvailableTotalPrice - unShippingAmount) < contractDeposit)
                {
                    List<string> gInitLocal2 = new List<string>();
                    gInitLocal2.Add((invAvailableTotalPrice - unShippingAmount).ToString());
                    gInitLocal2.Add(contractDeposit.ToString());
                    PublicMethod.GetInstance().ShowAlertMessage("F3020_001", this.pubParasInfo.UserLanguage, gInitLocal2);
                }
            }
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string lineID = string.Empty;
            try
            {
                if (ordersInfo.Type.ToUpper() == "TRANSFER")
                {
                    IntransitDetailInfo intransitDetailInfo = new IntransitDetailInfo {
                        IntransitID = ordersInfo.IntransitID,
                        LineID = orderDetailInfo.LineID,
                        OrderLineID = orderDetailInfo.LineID,
                        ReceiptLineID = 0,
                        OwnerID = orderDetailInfo.OwnerID,
                        SkuID = orderDetailInfo.SkuID,
                        UomID = orderDetailInfo.UomID,
                        PackID = orderDetailInfo.PackID,
                        Qty = orderDetailInfo.OriginalQty,
                        ShippedQty = orderDetailInfo.ShippedQty,
                        ReceivedQty = 0M,
                        Status = IntransitStatus.New,
                        CreatedBy = orderDetailInfo.CreatedBy,
                        CreatedDate = orderDetailInfo.CreatedDate,
                        UpdatedBy = orderDetailInfo.UpdatedBy,
                        UpdatedDate = orderDetailInfo.UpdatedDate
                    };
                    intransitDetailDA.InsertIntransitDetail(dataBase, tran, intransitDetailInfo, this.dataBaseID);
                    auditDataBLL.InsertAuditData<IntransitDetailInfo>(dataBase, tran, null, intransitDetailInfo, AuditAction.Insert, this.pubParasInfo.UserID, "INTRANSIT_DETAIL", this.dataBaseID[0]);
                }
                if (orderDetailDA.InsertOrderDetail(dataBase, tran, orderDetailInfo, this.dataBaseID[0]) > 0)
                {
                    auditDataBLL.InsertAuditData<OrderDetailInfo>(dataBase, tran, null, orderDetailInfo, AuditAction.Insert, this.pubParasInfo.UserID, "ORDER_DETAIL", this.dataBaseID[0]);
                    lineID = orderDetailInfo.LineID.ToString();
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return lineID;
        }

        public string ShippingOrderDetailImportByData(string[][] ImportByData)
        {
            int i;
            Exception ex;
            string LocIDTrue = string.Empty;
            string LocIDFalse = string.Empty;
            decimal contractDeposit = 0M;
            decimal invAvailableTotalPrice = 0M;
            decimal unShippingAmount = 0M;
            int iLineID = 0;
            TradePartnerInfo tradePartnerInfo = null;
            List<OrderDetailInfo> orderDetailInfoList = new List<OrderDetailInfo>();
            for (i = 1; ImportByData.Length > i; i++)
            {
                OrderDetailInfo orderDetail = new OrderDetailInfo();
                for (int n = 0; ImportByData[i].Length > n; n++)
                {
                    try
                    {
                        switch (ImportByData[0][n].ToUpper())
                        {
                            case "SKU":
                            {
                                if (!string.IsNullOrEmpty(ImportByData[i][n]))
                                {
                                    orderDetail.SkuID = ImportByData[i][n];
                                }
                                continue;
                            }
                            case "QTY":
                            {
                                if (!string.IsNullOrEmpty(ImportByData[i][n]))
                                {
                                    orderDetail.OriginalQty = Convert.ToDecimal(ImportByData[i][n]);
                                }
                                continue;
                            }
                            case "VENDOR ID":
                            {
                                if (!string.IsNullOrEmpty(ImportByData[i][n]))
                                {
                                    orderDetail.VendorID = ImportByData[i][n];
                                }
                                continue;
                            }
                            case "LOTTABLE01":
                            {
                                if (string.IsNullOrEmpty(ImportByData[i][n]))
                                {
                                    break;
                                }
                                orderDetail.LotAttr01 = ImportByData[i][n];
                                continue;
                            }
                            case "LOTTABLE02":
                            {
                                if (string.IsNullOrEmpty(ImportByData[i][n]))
                                {
                                    goto Label_037A;
                                }
                                orderDetail.LotAttr02 = ImportByData[i][n];
                                continue;
                            }
                            case "LOTTABLE03":
                            {
                                if (string.IsNullOrEmpty(ImportByData[i][n]))
                                {
                                    goto Label_03B3;
                                }
                                orderDetail.LotAttr03 = ImportByData[i][n];
                                continue;
                            }
                            case "LOTTABLE04":
                            {
                                if (string.IsNullOrEmpty(ImportByData[i][n]))
                                {
                                    goto Label_03EC;
                                }
                                orderDetail.LotAttr04 = ImportByData[i][n];
                                continue;
                            }
                            case "LOTTABLE05":
                            {
                                if (string.IsNullOrEmpty(ImportByData[i][n]))
                                {
                                    goto Label_0425;
                                }
                                orderDetail.LotAttr05 = ImportByData[i][n];
                                continue;
                            }
                            case "LOTTABLE06":
                            {
                                if (string.IsNullOrEmpty(ImportByData[i][n]))
                                {
                                    goto Label_045E;
                                }
                                orderDetail.LotAttr06 = ImportByData[i][n];
                                continue;
                            }
                            case "LOTTABLE07":
                            {
                                if (string.IsNullOrEmpty(ImportByData[i][n]))
                                {
                                    goto Label_0497;
                                }
                                orderDetail.LotAttr07 = ImportByData[i][n];
                                continue;
                            }
                            case "LOTTABLE08":
                            {
                                if (string.IsNullOrEmpty(ImportByData[i][n]))
                                {
                                    goto Label_04D0;
                                }
                                orderDetail.LotAttr08 = ImportByData[i][n];
                                continue;
                            }
                            case "LOTTABLE09":
                            {
                                if (string.IsNullOrEmpty(ImportByData[i][n]))
                                {
                                    goto Label_050E;
                                }
                                orderDetail.LotAttr09 = ImportByData[i][n].ToUpper();
                                continue;
                            }
                            case "LOTTABLE10":
                            {
                                if (string.IsNullOrEmpty(ImportByData[i][n]))
                                {
                                    goto Label_0547;
                                }
                                orderDetail.LotAttr10 = ImportByData[i][n];
                                continue;
                            }
                            case "RECEIPT DATE":
                            {
                                if (!string.IsNullOrEmpty(ImportByData[i][n]))
                                {
                                    orderDetail.ReceiptDate = Convert.ToDateTime(ImportByData[i][n]);
                                }
                                continue;
                            }
                            case "PRODUCE DATE":
                            {
                                if (!string.IsNullOrEmpty(ImportByData[i][n]))
                                {
                                    orderDetail.ProduceDate = Convert.ToDateTime(ImportByData[i][n]);
                                }
                                continue;
                            }
                            case "EXPIRY DATE":
                            {
                                if (!string.IsNullOrEmpty(ImportByData[i][n]))
                                {
                                    orderDetail.ExpiryDate = Convert.ToDateTime(ImportByData[i][n]);
                                }
                                continue;
                            }
                            case "EXTERNAL LOT":
                            {
                                if (!string.IsNullOrEmpty(ImportByData[i][n]))
                                {
                                    orderDetail.ExternalLot = ImportByData[i][n];
                                }
                                continue;
                            }
                            case "REMARK":
                            {
                                if (!string.IsNullOrEmpty(ImportByData[i][n]))
                                {
                                    orderDetail.Remark = ImportByData[i][n];
                                }
                                continue;
                            }
                            case "CREATEDBY":
                            {
                                if (!string.IsNullOrEmpty(ImportByData[i][n]))
                                {
                                    orderDetail.CreatedBy = ImportByData[i][n];
                                }
                                continue;
                            }
                            case "OWNERID":
                            {
                                if (!string.IsNullOrEmpty(ImportByData[i][n]))
                                {
                                    orderDetail.OwnerID = ImportByData[i][n];
                                }
                                continue;
                            }
                            case "ORDERID":
                                if (string.IsNullOrEmpty(ImportByData[i][n]))
                                {
                                    continue;
                                }
                                orderDetail.OrderID = ImportByData[i][n];
                                if (iLineID != 0)
                                {
                                    goto Label_06E1;
                                }
                                iLineID = commonMethod.GetMaxLineID("ORDER_DETAIL", orderDetail.OrderID, this.dataBaseID[0]);
                                iLineID += 10;
                                goto Label_06EA;

                            case "UDF1":
                            {
                                if (!string.IsNullOrEmpty(ImportByData[i][n]))
                                {
                                    orderDetail.Udf1 = ImportByData[i][n];
                                }
                                continue;
                            }
                            case "UDF2":
                            {
                                if (!string.IsNullOrEmpty(ImportByData[i][n]))
                                {
                                    orderDetail.Udf2 = ImportByData[i][n];
                                }
                                continue;
                            }
                            case "UDF3":
                            {
                                if (!string.IsNullOrEmpty(ImportByData[i][n]))
                                {
                                    orderDetail.Udf3 = ImportByData[i][n];
                                }
                                continue;
                            }
                            case "UDF4":
                            {
                                if (!string.IsNullOrEmpty(ImportByData[i][n]))
                                {
                                    orderDetail.Udf4 = ImportByData[i][n];
                                }
                                continue;
                            }
                            case "UDF5":
                            {
                                if (!string.IsNullOrEmpty(ImportByData[i][n]))
                                {
                                    orderDetail.Udf5 = ImportByData[i][n];
                                }
                                continue;
                            }
                            case "UDF6":
                            {
                                if (!string.IsNullOrEmpty(ImportByData[i][n]))
                                {
                                    orderDetail.Udf6 = ImportByData[i][n];
                                }
                                continue;
                            }
                            case "UDF7":
                            {
                                if (!string.IsNullOrEmpty(ImportByData[i][n]))
                                {
                                    orderDetail.Udf7 = ImportByData[i][n];
                                }
                                continue;
                            }
                            case "UDF8":
                            {
                                if (!string.IsNullOrEmpty(ImportByData[i][n]))
                                {
                                    orderDetail.Udf8 = ImportByData[i][n];
                                }
                                continue;
                            }
                            case "UDF9":
                            {
                                if (!string.IsNullOrEmpty(ImportByData[i][n]))
                                {
                                    orderDetail.Udf9 = ImportByData[i][n];
                                }
                                continue;
                            }
                            case "UDF10":
                            {
                                if (!string.IsNullOrEmpty(ImportByData[i][n]))
                                {
                                    orderDetail.Udf10 = ImportByData[i][n];
                                }
                                continue;
                            }
                            default:
                            {
                                continue;
                            }
                        }
                        orderDetail.LotAttr01 = "";
                        continue;
                    Label_037A:
                        orderDetail.LotAttr02 = "";
                        continue;
                    Label_03B3:
                        orderDetail.LotAttr03 = "";
                        continue;
                    Label_03EC:
                        orderDetail.LotAttr04 = "";
                        continue;
                    Label_0425:
                        orderDetail.LotAttr05 = "";
                        continue;
                    Label_045E:
                        orderDetail.LotAttr06 = "";
                        continue;
                    Label_0497:
                        orderDetail.LotAttr07 = "";
                        continue;
                    Label_04D0:
                        orderDetail.LotAttr08 = "";
                        continue;
                    Label_050E:
                        orderDetail.LotAttr09 = "OK";
                        continue;
                    Label_0547:
                        orderDetail.LotAttr10 = "";
                        continue;
                    Label_06E1:
                        iLineID += 10;
                    Label_06EA:
                        orderDetail.LineID = iLineID;
                    }
                    catch
                    {
                        if (!string.IsNullOrEmpty(orderDetail.OrderID))
                        {
                            throw new Exception(orderDetail.SkuID + "（数据内容异常）");
                        }
                    }
                }
                orderDetail.UpdatedBy = orderDetail.CreatedBy;
                orderDetail.ExternOrderID = "";
                orderDetail.ExternLineID = "";
                orderDetail.AltSkuID = "";
                orderDetail.OpenQty = 0M;
                orderDetail.ShippedQty = 0M;
                orderDetail.AdjustedQty = 0M;
                orderDetail.PreallocatedQty = 0M;
                orderDetail.AllocatedQty = 0M;
                orderDetail.PickedQty = 0M;
                orderDetail.PickCode = "";
                orderDetail.CartonGroup = "";
                orderDetail.LpnID = "";
                orderDetail.UnitPrice = 0.0;
                orderDetail.Tax01 = 0.0;
                orderDetail.Tax02 = 0.0;
                orderDetail.ExtendedPrice = 0.0;
                orderDetail.UpdateSource = "";
                orderDetail.ProductWeight = 0.0;
                orderDetail.ProductCube = 0.0;
                orderDetail.CartonBreak = "";
                orderDetail.CartonQtyBreak = 0;
                orderDetail.QcRequired = "";
                orderDetail.QcAutoAdjust = "";
                orderDetail.ShipGroup01 = "";
                orderDetail.ShipGroup02 = "";
                orderDetail.ShipGroup03 = "";
                orderDetail.TariffID = "";
                orderDetail.OriginalLineID = "";
                orderDetail.OriginalSku = "";
                orderDetail.BuyerPo = "";
                orderDetail.ActualShipDate = DateTime.Now;
                orderDetail.IsSubstitute = 0;
                orderDetail.OkToSubstitute = 0;
                orderDetail.PreallocateRuleID = "STD";
                orderDetail.AllocateRuleID = "STD";
                orderDetail.SkuRotation = "STD";
                orderDetail.AllocateStrategyType = "";
                orderDetail.ShelfLife = 0;
                orderDetail.Rotation = "";
                orderDetail.SubFlag = "";
                orderDetail.WorkOrderID = "";
                orderDetail.LineType = "";
                orderDetail.ComponentQty = 0M;
                orderDetail.ComponentReference = "";
                orderDetail.Lot = "";
                orderDetail.PalletID = "";
                orderDetail.OrigCaseQty = 0M;
                orderDetail.OrigPalletQty = 0M;
                orderDetail.OppRequest = "";
                orderDetail.PickingInstructions = "";
                orderDetail.QtyIntransit = 0M;
                orderDetail.WpReleased = "";
                orderDetail.ExternalLocSequence = "";
                orderDetail.MinShipperCent = 0;
                orderDetail.Status = "NEW";
                orderDetail.EffectiveDate = DateTime.Now;
                if (!string.IsNullOrEmpty(orderDetail.OrderID))
                {
                    if (tradePartnerInfo == null)
                    {
                        tradePartnerInfo = ownerDA.GetTradePartnerByID(orderDetail.OwnerID, this.dataBaseID[0]);
                    }
                    SkuInfo skuInfo = skuDA.GetSkuByID(orderDetail.OwnerID, orderDetail.SkuID, this.dataBaseID[0]);
                    if (skuInfo == null)
                    {
                        throw new Exception(string.Concat(new object[] { "行号 ", orderDetail.LineID, " 货品", orderDetail.SkuID, "（不存在,导入失败）" }));
                    }
                    if (skuInfo.IsActive != "Y")
                    {
                        throw new Exception(string.Concat(new object[] { "行号 ", orderDetail.LineID, " 货品", orderDetail.SkuID, "（未启用,导入失败）" }));
                    }
                    if ((tradePartnerInfo.Finance_Logistical_Ctrl == "Y") && (tradePartnerInfo.Finance_Logistical_Calc_By.ToUpper() == "SKUPRICE"))
                    {
                        ContractInfo contractInfo = contractDA.GetContractByOwnerID(orderDetail.OwnerID, "Y", this.dataBaseID[0]);
                        if (contractInfo == null)
                        {
                            contractDeposit = 0M;
                        }
                        else
                        {
                            contractDeposit = contractInfo.Deposit;
                        }
                        invAvailableTotalPrice = salesOrderDetailDA.GetOwnerAllInvDeposit(orderDetail.OwnerID, this.dataBaseID[0]);
                        unShippingAmount = salesOrderDetailDA.GetAllOrderDetailUnshippingAmount(orderDetail.OwnerID, this.dataBaseID[0]) + (skuInfo.CostPrice * orderDetail.OriginalQty);
                        if ((invAvailableTotalPrice - unShippingAmount) < contractDeposit)
                        {
                            List<string> gInitLocal4 = new List<string>();
                            gInitLocal4.Add((invAvailableTotalPrice - unShippingAmount).ToString());
                            gInitLocal4.Add(contractDeposit.ToString());
                            PublicMethod.GetInstance().ShowAlertMessage("F3020_001", this.pubParasInfo.UserLanguage, gInitLocal4);
                        }
                    }
                    try
                    {
                        orderDetail.PackID = skuInfo.PackID;
                        PackInfo packInfo = packDA.GetPackByID(orderDetail.PackID, this.dataBaseID[0]);
                        orderDetail.UomID = packInfo.PackUom1;
                        if (!string.IsNullOrEmpty(skuInfo.PreallocateRuleID))
                        {
                            orderDetail.PreallocateRuleID = skuInfo.PreallocateRuleID;
                        }
                        if (!string.IsNullOrEmpty(skuInfo.AllocateRuleID))
                        {
                            orderDetail.AllocateRuleID = skuInfo.AllocateRuleID;
                        }
                        if (!string.IsNullOrEmpty(skuInfo.RotationRuleID))
                        {
                            orderDetail.SkuRotation = skuInfo.RotationRuleID;
                        }
                        orderDetailInfoList.Add(orderDetail);
                    }
                    catch (Exception exception1)
                    {
                        ex = exception1;
                        throw new Exception(orderDetail.LineID + "（数据交互时出现异常）" + ex.Message);
                    }
                }
            }
            if ((orderDetailInfoList != null) && (orderDetailInfoList.Count > 0))
            {
                DataBase dataBase = new DataBase();
                DbConnection conn = dataBase.connection;
                conn.Open();
                DbTransaction tran = conn.BeginTransaction();
                try
                {
                    for (i = 0; i < orderDetailInfoList.Count; i++)
                    {
                        orderDetailDA.InsertOrderDetail(dataBase, tran, orderDetailInfoList[i], this.dataBaseID[0]);
                        LocIDTrue = LocIDTrue + orderDetailInfoList[i].LineID + ";";
                    }
                    tran.Commit();
                }
                catch (Exception exception2)
                {
                    ex = exception2;
                    tran.Rollback();
                    CommonErrorHandler.ErrorHandler(ex, LogType.Error);
                }
                finally
                {
                    conn.Close();
                }
            }
            return LocIDFalse;
        }

        public string UpdateOrderDetail(OrderDetailInfo orderDetailInfo)
        {
            orderDetailInfo.UpdatedBy = this.pubParasInfo.UserID;
            AuditDataBLL auditDataBLL = new AuditDataBLL();
            orderDetailInfo.OriginalQty = new InboundHelper().ExchangeUom(orderDetailInfo.PackID, orderDetailInfo.UomID, orderDetailInfo.OriginalQty, "W", this.dataBaseID[0]);
            decimal contractDeposit = 0M;
            decimal invAvailableTotalPrice = 0M;
            decimal unShippingAmount = 0M;
            TradePartnerInfo tradePartnerInfo = ownerDA.GetTradePartnerByID(orderDetailInfo.OwnerID, this.dataBaseID[0]);
            if ((tradePartnerInfo.Finance_Logistical_Ctrl == "Y") && (tradePartnerInfo.Finance_Logistical_Calc_By.ToUpper() == "SKUPRICE"))
            {
                ContractInfo contractInfo = contractDA.GetContractByOwnerID(orderDetailInfo.OwnerID, "Y", this.dataBaseID[0]);
                if (contractInfo == null)
                {
                    contractDeposit = 0M;
                }
                else
                {
                    contractDeposit = contractInfo.Deposit;
                }
                invAvailableTotalPrice = salesOrderDetailDA.GetOwnerAllInvDeposit(orderDetailInfo.OwnerID, this.dataBaseID[0]);
                unShippingAmount = salesOrderDetailDA.GetAllOrderDetailUnshippingAmount(orderDetailInfo.OwnerID, this.dataBaseID[0]);
                SkuInfo skuInfo = skuDA.GetSkuByID(orderDetailInfo.OwnerID, orderDetailInfo.SkuID, this.dataBaseID[0]);
                unShippingAmount += skuInfo.CostPrice * orderDetailInfo.OriginalQty;
                if ((invAvailableTotalPrice - unShippingAmount) < contractDeposit)
                {
                    List<string> gInitLocal3 = new List<string>();
                    gInitLocal3.Add((invAvailableTotalPrice - unShippingAmount).ToString());
                    gInitLocal3.Add(contractDeposit.ToString());
                    PublicMethod.GetInstance().ShowAlertMessage("F3020_001", this.pubParasInfo.UserLanguage, gInitLocal3);
                }
            }
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string lineID = string.Empty;
            try
            {
                if (!string.IsNullOrEmpty(orderDetailInfo.IntransitID))
                {
                    IntransitDetailInfo tempIntransitDetailInfo = intransitDetailDA.GetIntransitDetailByID(dataBase, tran, orderDetailInfo.IntransitID, orderDetailInfo.LineID.ToString(), this.dataBaseID[0]);
                    if (tempIntransitDetailInfo != null)
                    {
                        IntransitDetailInfo intransitDetailInfo = new IntransitDetailInfo();
                        intransitDetailInfo = tempIntransitDetailInfo;
                        intransitDetailInfo.IntransitID = orderDetailInfo.IntransitID;
                        intransitDetailInfo.LineID = orderDetailInfo.LineID;
                        intransitDetailInfo.OrderLineID = orderDetailInfo.LineID;
                        intransitDetailInfo.ReceiptLineID = 0;
                        intransitDetailInfo.OwnerID = orderDetailInfo.OwnerID;
                        intransitDetailInfo.SkuID = orderDetailInfo.SkuID;
                        intransitDetailInfo.UomID = orderDetailInfo.UomID;
                        intransitDetailInfo.PackID = orderDetailInfo.PackID;
                        intransitDetailInfo.Qty = orderDetailInfo.OriginalQty;
                        intransitDetailInfo.ShippedQty = orderDetailInfo.ShippedQty;
                        intransitDetailInfo.ReceivedQty = 0M;
                        intransitDetailInfo.Status = IntransitStatus.New;
                        intransitDetailInfo.UpdatedBy = orderDetailInfo.UpdatedBy;
                        intransitDetailDA.UpdateIntransitDetail(dataBase, tran, intransitDetailInfo, this.dataBaseID);
                        auditDataBLL.InsertAuditData<IntransitDetailInfo>(dataBase, tran, tempIntransitDetailInfo, intransitDetailInfo, AuditAction.Update, this.pubParasInfo.UserID, "INTRANSIT_DETAIL", this.dataBaseID[0]);
                    }
                }
                OrderDetailInfo oldOrderDetailInfo = orderDetailDA.GetOrderDetailByID(dataBase, tran, orderDetailInfo.OrderID, orderDetailInfo.LineID.ToString(), this.dataBaseID[0], this.pubParasInfo);
                if (orderDetailDA.UpdateOrderDetail(dataBase, tran, orderDetailInfo, this.dataBaseID[0]) > 0)
                {
                    auditDataBLL.InsertAuditData<OrderDetailInfo>(dataBase, tran, oldOrderDetailInfo, orderDetailInfo, AuditAction.Update, this.pubParasInfo.UserID, "ORDER_DETAIL", this.dataBaseID[0]);
                    lineID = orderDetailInfo.LineID.ToString();
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return lineID;
        }
    }
}

